package com.ymatou.iapi.trading.sellerquery2.parameter.sp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;

/**
 * sp_trd_GetOrderFlows
 * 
 * @author songyefeng
 *
 */
public class Sp_GetOrderFlows {
	private Integer orderId;

	public void setOrderId(Integer orderId) {
		this.orderId = orderId;
	}

	@SuppressWarnings("rawtypes")
	public void execute() {

		DefaultWapper db = new DefaultWapper();
		List<Map> rs1 = db
				.executeSql("select iorderid as orderId, bshangouorder as shangou, iTradingStatus as status, iSalesRefundStatus as refund from ymt_orders(nolock) where iorderid = "
						+ DataUtil.Integerr.GetValueOrDefault(orderId));
		if (false == DataUtil.isNullOrEmpty(rs1)) {
			Map order = rs1.get(0);
			OrderInfo = new com.ymatou.query.seller.trading.api.parameter.GetOrderFlowsResult.SimpleOrderInfo();
			DataUtil.mapToBean(OrderInfo, order);
			if (OrderInfo.getStatus() == null) {
				return;
			}
			Flows = new ArrayList<com.ymatou.iapi.trading.sellerquery.parameter.orderquery.GetOrderFlowsResult.FlowsDetail>();
			int s = OrderInfo.getStatus();
			if (OrderInfo.getShangou()) {
				if (s == 2 || s == 3 || s == 4 || s == 16 || s == 17) {
					String sql1 = "SELECT"
							+ " TOP 1 o.dPaidTime AS time,"
							+ " s.fPaidAmountOfCash AS cash,"
							+ " fPaidAmountOfGift AS gift,"
							+ " fPaidAmountOfFreeCard AS freeCard,"
							+ " CASE"
							+ " WHEN s.fPaidAmountOfCoupon > 0 THEN"
							+ " ("
							+ " CASE o.iCouponChannel"
							+ " WHEN 2 THEN"
							+ " - s.fPaidAmountOfCoupon"
							+ " ELSE"
							+ " s.fPaidAmountOfCoupon"
							+ " END"
							+ " )"
							+ " ELSE"
							+ " 0"
							+ " END as coupon,"
//							+ " o.iCouponChannel as couponChannel,"
							+ " case when (o.fYmtCouponAmount is null or o.fYmtCouponAmount < 0.01) and (o.fSellerCouponAmount is null or o.fSellerCouponAmount < 0.01) then 0 "
							+ " when (o.fYmtCouponAmount > 0.00) and (o.fSellerCouponAmount is null or o.fSellerCouponAmount < 0.01) then 1"
							+ " when (o.fYmtCouponAmount is null or o.fYmtCouponAmount < 0.01) and o.fSellerCouponAmount > 0.00 then 2"
							+"  else 3 end as couponChannel,"
							+ " o.sCouponCode as couponCode,"
							+ " s.fPaidAmountOfCash + fPaidAmountOfGift + fPaidAmountOfFreeCard + ("
							+ " CASE"
							+ " WHEN s.fPaidAmountOfCoupon > 0 THEN"
							+ " ("
							+ " CASE o.iCouponChannel"
							+ " WHEN 2 THEN"
							+ " 0"
							+ " ELSE"
							+ " s.fPaidAmountOfCoupon"
							+ " END"
							+ " )"
							+ " ELSE"
							+ " 0"
							+ " END"
							+ " ) as total,"
							+ " o.fOrderPrice as occurredPrice,"
							+ " 0 as priceChange,"
							+ " o.fFreight as freight,"
							+ " NULL as evidence,"
							+ " 1 as isIncoming,"
							+ " 1 as operation"
							+ " FROM"
							+ " Ymt_OrderState (nolock) s"
							+ " JOIN Ymt_Orders (nolock) o ON s.iorderid = o.iorderid"
							+ " where s.iorderid = " + orderId;
					List<Map> t1 = db.executeSql(sql1);
					if (false == DataUtil.isNullOrEmpty(t1)) {
						com.ymatou.iapi.trading.sellerquery.parameter.orderquery.GetOrderFlowsResult.FlowsDetail fd1 = new com.ymatou.iapi.trading.sellerquery.parameter.orderquery.GetOrderFlowsResult.FlowsDetail();
						DataUtil.mapToBean(fd1, t1.get(0));
						Flows.add(fd1);
					}
				}
				if (s == 3 || s == 4 || s == 17) {
					String sql2 = "SELECT"
							+ " TOP 1 o.dPostPaidTime AS time,"
							+ " s.fPostPaidAmountOfCash AS cash,"
							+ " fPostPaidAmountOfGift AS gift,"
							+ " 0 AS freeCard,"
							+ " 0 AS coupon,"
							+ " case when (o.fYmtCouponAmount is null or o.fYmtCouponAmount < 0.01) and (o.fSellerCouponAmount is null or o.fSellerCouponAmount < 0.01) then 0 "
							+ " when (o.fYmtCouponAmount > 0.00) and (o.fSellerCouponAmount is null or o.fSellerCouponAmount < 0.01) then 1"
							+ " when (o.fYmtCouponAmount is null or o.fYmtCouponAmount < 0.01) and o.fSellerCouponAmount > 0.00 then 2"
							+"  else 3 end as couponChannel,"
							+ " NULL AS couponCode,"
							+ " s.fPostPaidAmountOfCash + fPostPaidAmountOfGift AS total,"
							+ " s.fPostPaidAmountOfCash + fPostPaidAmountOfGift AS occurredPrice,"
							+ " s.fPostPaidAmountOfCash + fPostPaidAmountOfGift - ("
							+ " o.fTotalPrice - o.fOrderPrice"
							+ " ) AS priceChange,"
							+ " 0 AS freight,"
							+ " NULL AS evidence,"
							+ " 1 AS isIncoming,"
							+ " 2 AS operation"
							+ " FROM"
							+ " Ymt_OrderState (nolock) s"
							+ " JOIN Ymt_Orders (nolock) o ON s.iorderid = o.iorderid"
							+ " WHERE" + " o.bPaidInFull = 1"
							+ " AND s.fPostPaidAmountOfCash > 0"
							+ " AND s.iorderid = " + orderId;
					List<Map> t2 = db.executeSql(sql2);
					if (false == DataUtil.isNullOrEmpty(t2)) {
						com.ymatou.iapi.trading.sellerquery.parameter.orderquery.GetOrderFlowsResult.FlowsDetail fd2 = new com.ymatou.iapi.trading.sellerquery.parameter.orderquery.GetOrderFlowsResult.FlowsDetail();
						DataUtil.mapToBean(fd2, t2.get(0));
						Flows.add(fd2);
					}
				}
				if (DataUtil.Integerr.GetValueOrDefault(OrderInfo.getRefund()) == 10) {
					String sql3 = "SELECT" + " TOP 1 AddTime AS time,"
							+ " - RefundedAmountOfCash AS cash,"
							+ " - RefundedAmountOfGift AS gift,"
							+ " 0 AS freeCard," 
							+ " 0 AS coupon,"
							+ " 0 AS couponChannel," 
							+ " NULL AS couponCode,"
							+ " - RefundAmount AS total,"
							+ " - RefundAmount AS occurredPrice,"
							+ " 0 AS priceChange," + " 0 AS freight,"
							+ " RefundBillNo AS evidence,"
							+ " 0 AS isIncoming," 
							+ " 3 AS operation" 
							+ " FROM"
							+ " Ymt_RefundBill (nolock)"
							+ " where RefundStatus = 1 and orderid = " + orderId;
					List<Map> t3 = db.executeSql(sql3);
					if (false == DataUtil.isNullOrEmpty(t3)) {
						com.ymatou.iapi.trading.sellerquery.parameter.orderquery.GetOrderFlowsResult.FlowsDetail fd3 = new com.ymatou.iapi.trading.sellerquery.parameter.orderquery.GetOrderFlowsResult.FlowsDetail();
						DataUtil.mapToBean(fd3, t3.get(0));
						Flows.add(fd3);
					}
				}
			} else {
				if (s == 2 || s == 3 || s == 4 || s == 16 || s == 17) {
					String sql4 = "SELECT"
							+ " TOP 1 o.dPaidTime AS time,"
							+ " s.fPaidAmountOfCash AS cash,"
							+ " fPaidAmountOfGift AS gift,"
							+ " fPaidAmountOfFreeCard AS freeCard,"
							+ " CASE"
							+ " WHEN s.fPaidAmountOfCoupon > 0 THEN"
							+ " ("
							+ " CASE o.iCouponChannel"
							+ " WHEN 2 THEN"
							+ " - s.fPaidAmountOfCoupon"
							+ " ELSE"
							+ " s.fPaidAmountOfCoupon"
							+ " END"
							+ " )"
							+ " ELSE"
							+ " 0"
							+ " END as coupon,"
							+ " case when (o.fYmtCouponAmount is null or o.fYmtCouponAmount < 0.01) and (o.fSellerCouponAmount is null or o.fSellerCouponAmount < 0.01) then 0 "
							+ " when (o.fYmtCouponAmount > 0.00) and (o.fSellerCouponAmount is null or o.fSellerCouponAmount < 0.01) then 1"
							+ " when (o.fYmtCouponAmount is null or o.fYmtCouponAmount < 0.01) and o.fSellerCouponAmount > 0.00 then 2"
							+"  else 3 end as couponChannel,"
							+ " o.sCouponCode as couponCode,"
							+ " s.fPaidAmountOfCash + fPaidAmountOfGift + fPaidAmountOfFreeCard + ("
							+ " CASE"
							+ " WHEN s.fPaidAmountOfCoupon > 0 THEN"
							+ " ("
							+ " CASE o.iCouponChannel"
							+ " WHEN 2 THEN"
							+ " 0"
							+ " ELSE"
							+ " s.fPaidAmountOfCoupon"
							+ " END"
							+ " )"
							+ " ELSE"
							+ " 0"
							+ " END"
							+ " ) as total,"
							+ " o.fOrderPrice as occurredPrice,"
							+ " o.fOrderDiscount as priceChange,"
							+ " o.fFreight as freight,"
							+ " NULL as evidence,"
							+ " 1 as isIncoming,"
							+ " 1 as operation"
							+ " FROM"
							+ " Ymt_OrderState (nolock) s"
							+ " JOIN Ymt_Orders (nolock) o ON s.iorderid = o.iorderid"
							+ " where s.iorderid = " + orderId;
					List<Map> t4 = db.executeSql(sql4);
					if (false == DataUtil.isNullOrEmpty(t4)) {
						com.ymatou.iapi.trading.sellerquery.parameter.orderquery.GetOrderFlowsResult.FlowsDetail fd4 = new com.ymatou.iapi.trading.sellerquery.parameter.orderquery.GetOrderFlowsResult.FlowsDetail();
						DataUtil.mapToBean(fd4, t4.get(0));
						Flows.add(fd4);
					}
				}
				if (DataUtil.Integerr.GetValueOrDefault(OrderInfo.getRefund()) == 10) {
					String sql5 = "SELECT" + " TOP 1 AddTime AS time,"
							+ " - RefundedAmountOfCash AS cash,"
							+ " - RefundedAmountOfGift AS gift,"
							+ " 0 AS freeCard," 
							+ " 0 AS coupon,"
							+ " 0 AS couponChannel," 
							+ " NULL AS couponCode,"
							+ " - RefundAmount AS total,"
							+ " - RefundAmount AS occurredPrice,"
							+ " 0 AS priceChange," 
							+ " 0 AS freight,"
							+ " RefundBillNo AS evidence,"
							+ " 0 AS isIncoming," 
							+ " 3 AS operation" 
							+ " FROM"
							+ " Ymt_RefundBill (nolock)"
							+ " where RefundStatus = 1 and orderid = " + orderId;
					List<Map> t5 = db.executeSql(sql5);
					if (false == DataUtil.isNullOrEmpty(t5)) {
						com.ymatou.iapi.trading.sellerquery.parameter.orderquery.GetOrderFlowsResult.FlowsDetail fd5 = new com.ymatou.iapi.trading.sellerquery.parameter.orderquery.GetOrderFlowsResult.FlowsDetail();
						DataUtil.mapToBean(fd5, t5.get(0));
						Flows.add(fd5);
					}
				}
			}
		}
	}

	public com.ymatou.query.seller.trading.api.parameter.GetOrderFlowsResult.SimpleOrderInfo getOrderInfo() {
		return OrderInfo;
	}

	public List<com.ymatou.iapi.trading.sellerquery.parameter.orderquery.GetOrderFlowsResult.FlowsDetail> getFlows() {
		return Flows;
	}

	private com.ymatou.query.seller.trading.api.parameter.GetOrderFlowsResult.SimpleOrderInfo OrderInfo;
	private List<com.ymatou.iapi.trading.sellerquery.parameter.orderquery.GetOrderFlowsResult.FlowsDetail> Flows;

	public static void main(String[] args) {
		Sp_GetOrderFlows sp = new Sp_GetOrderFlows();
		sp.setOrderId(102203094);
		sp.execute();
		System.out.println();
	}
}
